home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / MovieToolBox / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-09  |  12.5 KB  |  556 lines  |  [TEXT/KAHL]

  1. /* File:     main.c
  2.  
  3.  
  4. The main program structure:
  5.     
  6.     int FixMenus()
  7.     - Activate/Deactive menu as needed
  8.     
  9.     Edit Menu Stubs -- null routines...
  10.     
  11.     DoCommand(long    mResult)
  12.     - Handle all memu commands
  13.     
  14.     Boolean HandleEvents(EventRecord *myEvent);
  15.  
  16.     Initialize()
  17.     - Setup up menus and Macintosh enviroment
  18.  
  19.     Main()    
  20.     - Setup QuickTime movie enviroment
  21.     - Call Initialision routines
  22.     - Call Main Event Handling routines
  23.     - Cleanup QuickTime Movie Enviroment
  24.     
  25.     History..
  26.                     RMF    Created from DTS from sample source code.
  27.     Feb 9th,1994    RMF    Add click in context for next preview image
  28.                         Fix up Menu structure (eg. Direction menu, Effect Menu).
  29.                         Added BackGround Color option on Slide
  30. */
  31.  
  32. #include "gGlobals.h"
  33. #include <Picker.h>
  34. #include    <pascal.h>
  35. #include    <Scrap.h>
  36. #include    <Desk.h>
  37. #include    <string.h>
  38. #include    <stdio.h>
  39. #include    <Fonts.h>
  40. #include    <Math.h>
  41.  
  42. /* *****************************************************************************
  43.  
  44.     The following three routines are here because the MPW 3.2 c compiler 
  45.     screw up compiling the DoMovie routine if you use the "straight" calls.
  46.     
  47.     Try it and see and complain to DTS.
  48. */
  49.  
  50. void DoHLock(Handle h)
  51. {
  52.     HLock(h);
  53. }
  54. void DoHUnlock(Handle h)
  55. {
  56.     HUnlock( h);
  57. }
  58.  
  59. void DoDisposHandle(Handle h)
  60. {
  61.     DisposHandle(h);
  62. }
  63.  
  64. /*********************************************
  65.  
  66.     Clean up menu hiliting
  67.     
  68. *********************************************/
  69.  
  70. int FixMenus()
  71. {
  72.     Boolean    gotAllWindows;
  73.     Boolean    gotAnyWindows;
  74.  
  75.     gotAnyWindows = (gSrcWindow != nil || gAltWindow != nil);
  76.     gotAllWindows = gSrcWindow != nil &&
  77.                     (!gRequiresAlternate || gAltWindow != nil );
  78.     DisableItem(gMenus[FILE_MENU],FILE_M_SAVE);    
  79.     if ( gotAnyWindows )
  80.         EnableItem(gMenus[FILE_MENU],FILE_M_CLOSE);
  81.     else
  82.         DisableItem(gMenus[FILE_MENU],FILE_M_CLOSE);
  83.     
  84.     if (gotAllWindows ) 
  85.         DisableItem(gMenus[FILE_MENU],FILE_M_OPEN);
  86.     else 
  87.         EnableItem(gMenus[FILE_MENU],FILE_M_OPEN);
  88.  
  89.     if ( !gotAllWindows ) {
  90.         DisableItem(gMenus[MOVIE_MENU],MOVIE_M_APPEND);
  91.         DisableItem(gMenus[MOVIE_MENU],MOVIE_M_MOVIE);
  92.         DisableItem(gMenus[MOVIE_MENU],MOVIE_M_PREVIEW);
  93.         DisableItem(gMenus[MOVIE_MENU],MOVIE_M_OVERSAMPLE);
  94.     } else {
  95.         EnableItem(gMenus[MOVIE_MENU],MOVIE_M_APPEND);
  96.         EnableItem(gMenus[MOVIE_MENU],MOVIE_M_MOVIE);
  97.         EnableItem(gMenus[MOVIE_MENU],MOVIE_M_PREVIEW);
  98.         EnableItem(gMenus[MOVIE_MENU],MOVIE_M_OVERSAMPLE);
  99.     }
  100.     
  101.     DisableItem(gMenus[EDIT_MENU],EDIT_M_CUT);
  102.     DisableItem(gMenus[EDIT_MENU],EDIT_M_COPY);
  103.     DisableItem(gMenus[EDIT_MENU],EDIT_M_PASTE);
  104.     
  105.     DisableItem(gMenus[EDIT_MENU],EDIT_M_CLEAR);
  106.     DisableItem(gMenus[EDIT_MENU],EDIT_M_UNDO);
  107.     CheckItem(gMenus[MOVIE_MENU],MOVIE_M_OVERSAMPLE,gOversample);
  108.     
  109.     CheckItem(gMenus[Direction_Menu],EFFECT_BACKWARDS,gReverse);
  110.     CheckItem(gMenus[Direction_Menu],gEffectDirection,true);
  111.     CheckItem(gMenus[EFFECT_MENU],gEffectType,true);
  112.                 
  113. }
  114.  
  115. Boolean OptionDown(void)
  116. {    KeyMap k;
  117.     GetKeys(&k);
  118.     return (BitTst(&k, 61));        /* true if option is down */
  119. }    /* End of OptionDown() */
  120.  
  121. Boolean ShiftDown(void)
  122. {    KeyMap k;
  123.     GetKeys(&k);
  124.     return (BitTst(&k, 48));        /* true if shift is down */
  125. }    /* End of ShiftDown() */
  126.  
  127. /*********************************************
  128.  
  129.     Stubs
  130.     
  131. ********************************************/
  132.  
  133. OSErr DoCopy()
  134. {
  135.  
  136. }
  137.  
  138. OSErr DoCut()
  139. {
  140. }
  141.  
  142.  
  143. OSErr DoPaste()
  144. {
  145.  
  146. }
  147.  
  148. void DoClear()
  149. {
  150.  
  151.  
  152. }
  153.  
  154. OSErr DoUndo()
  155. {
  156.  
  157.  
  158. }
  159.  
  160.  
  161. /*********************************************
  162.  
  163.     Process menu command.
  164.     
  165. ********************************************/
  166.  
  167. DoCommand(long    mResult)
  168. {
  169.     short         theMenu, theItem;
  170.     Str255        daName;
  171.     GDHandle    saveGD;
  172.     CGrafPtr    savePort;
  173.     OSErr        res = 0;
  174.  
  175. #define HIshort(aLong)        (((aLong) >> 16) & 0xFFFF)
  176. #define LOshort(aLong)        ((aLong) & 0xFFFF)
  177.  
  178.     theItem = LOshort(mResult);
  179.     theMenu = HIshort(mResult);        /* This is the resource ID */
  180.  
  181.  
  182.     switch (theMenu) {
  183.         case APPLE_MENU_ID:
  184.             if (theItem == 1) {
  185.                 ShowAboutBox();
  186.             } else {
  187.                 GetItem(gMenus[0], theItem, daName);
  188.                 GetGWorld(&savePort,&saveGD);
  189.                 (void) OpenDeskAcc(daName);
  190.                 SetGWorld(savePort,saveGD);
  191.             }
  192.             break;
  193.  
  194.         case FILE_MENU_ID:
  195.             {    switch (theItem) {
  196.                 case FILE_M_OPEN:    DoOpen(nil);    FixMenus();    break;
  197.                 case FILE_M_CLOSE:    DoClose(gActiveWindow);    FixMenus();    res = 1;
  198.                     break;
  199.                 case FILE_M_SAVE:    break;
  200.                 
  201.                 case fileImport:    importMovie();    break;
  202.                 case fileExport:    exportMovie();    break;
  203.                             
  204.                     
  205.                 case FILE_M_QUIT:
  206.                     {    Boolean abortion = false;
  207.                         while ( gActiveWindow ) {
  208.                             if ( DoClose(gActiveWindow) < 0 ) {
  209.                                 abortion = true;    break;
  210.                                 }
  211.                         }
  212.                         if ( !abortion )    gExitFlag = true;            /* Request exit */
  213.                         res = 1;
  214.                         break;
  215.                     }
  216.                 }
  217.             }
  218.             break;
  219.             
  220.         /******************************/
  221.             
  222.         case EDIT_MENU_ID:
  223.             if ( !SystemEdit(theItem-1) ) {
  224.                 switch ( theItem ) {
  225.                 case EDIT_M_UNDO:    DoUndo();    break;
  226.                 case EDIT_M_CUT:    DoCut();    break;
  227.                 case EDIT_M_COPY:    DoCopy();    break;
  228.                 case EDIT_M_PASTE:    DoPaste();    break;
  229.                 case EDIT_M_CLEAR:    DoClear();    break;
  230.                 }
  231.             }
  232.             break;
  233.             
  234.         /******************************/
  235.         case Direction_MenuID:
  236.             
  237.             if (EFFECT_BACKWARDS == theItem) {
  238.                 gReverse = !gReverse;
  239.             } else {
  240.                 CheckItem(gMenus[Direction_Menu],gEffectDirection,false);
  241.                 gEffectDirection = theItem;
  242.                 }
  243.             break;
  244.             
  245.         case EFFECT_MENU_ID:
  246.             switch (theItem) {
  247.             case EFFECT_CrossFad:    
  248.                 gStandardP.depth = 32;        // randomdotstereogram needs 8bit grayscale buffers (40), 32 = RGBa color
  249.                 gRequiresAlternate = true;                // needs two picts to do processing
  250.                 break;
  251.             case EFFECT_Slide:
  252.                 gStandardP.depth = 32;        // randomdotstereogram needs 8bit grayscale buffers (40), 32 = RGBa color
  253.                 gRequiresAlternate = false;                // only needs one pict to do processing
  254.                 break;
  255.             case EFFECT_Roll:
  256.                 gStandardP.depth = 32;        // randomdotstereogram needs 8bit grayscale buffers (40), 32 = RGBa color
  257.                 gRequiresAlternate = false;                // only needs one pict to do processing
  258.                 break;
  259.                 
  260.             case EFFECT_StereoGram:        
  261.                 gStandardP.depth = 40;        // randomdotstereogram needs 8bit grayscale buffers (40), 32 = RGBa color
  262.                 gRequiresAlternate = false;                // only needs one pict to do processing
  263.                 break;
  264.                 
  265.             case EFFECT_BACKColor:
  266.             {    Point where;
  267.                 RGBColor outColor;
  268.                 SetPt(&where, 32, 32);
  269.                 if (GetColor(where,"\pSelect Color", &BACKColor, &outColor)) {
  270.                     BACKColor = outColor;
  271.                     }
  272.                 }
  273.             }    /* End switch */
  274.             
  275.             if (EFFECT_BACKColor != theItem) {
  276.                 CheckItem(gMenus[EFFECT_MENU],gEffectType,false);
  277.                 gEffectType = theItem;
  278.                 
  279.                 gStandardP.flags = scShowMotionSettings;
  280.                 gStandardP.theCodecType = kCodecType;
  281.                 gStandardP.theCodec = kCodecID;
  282.                 gStandardP.spatialQuality = kCodecQuality;
  283.                 gStandardP.temporalQuality = 0;
  284.                 gStandardP.depth = kCodecDepth;
  285.                 gStandardP.frameRate = kFrameRate<<16;
  286.                 gStandardP.keyFrameRate = kFrameRate;
  287.                 }
  288.             break;
  289.             
  290.         /******************************/
  291.             
  292.         case MOVIE_MENU_ID:
  293.             switch ( theItem ) {
  294.             case MOVIE_M_COMPRESS :        SetCompression();
  295.                 break;
  296.                 
  297.             case MOVIE_M_STAGES :        SetStages();
  298.                 break;
  299.                 
  300.             case MOVIE_M_OVERSAMPLE:    gOversample = !gOversample;
  301.                 break;
  302.                 
  303.             case MOVIE_M_PREVIEW :        DoPreview(true);
  304.                 break;
  305.                 
  306.             case MOVIE_M_MOVIE :        DoMovie(false);
  307.                 break;
  308.                 
  309.             case MOVIE_M_APPEND :        DoMovie(true);
  310.                 break;
  311.             }
  312.             break;
  313.     }/*endsw theMenu*/
  314.  
  315.     HiliteMenu(0);
  316.     return(res);
  317. }    /* End of () */
  318.  
  319.  
  320. /********************************************
  321.  
  322.     Process events.
  323.     
  324. ********************************************/
  325.  
  326. Boolean HandleEvents(EventRecord *myEvent)
  327. {    Rect        dragRect;
  328.     WindowPtr    whichWindow;
  329.     short        res = 0;
  330.     GWorldPtr    saveWorld;
  331.     GDHandle    saveGD;
  332.  
  333.     GetGWorld(&saveWorld,&saveGD);
  334.  
  335.     switch (myEvent->what) {
  336.     case mouseDown:
  337.         
  338.         switch ((short)FindWindow(myEvent->where, &whichWindow)) {
  339.         case inSysWindow:    SystemClick(myEvent, whichWindow);
  340.             break;
  341.             
  342.         case inMenuBar:    
  343.             FixMenus();
  344.             res = DoCommand(MenuSelect(myEvent->where));
  345.             break;
  346.  
  347.         case inDrag:
  348.             SetPort((GrafPtr)whichWindow);
  349.             SetRect(&dragRect, 4, 20 + 4, qd.screenBits.bounds.right-4, qd.screenBits.bounds.bottom-4);
  350.             DragWindow(whichWindow, myEvent->where, &dragRect);
  351.             break;
  352.  
  353.         case inGrow:    break;
  354.         case inZoomIn:    break;
  355.         case inZoomOut:    break;
  356.  
  357.         case inGoAway:
  358.             if ( TrackGoAway(whichWindow,myEvent->where)  ) {
  359.                 DoClose(whichWindow);    res = true;
  360.                 }
  361.             break;
  362.  
  363.  
  364.         case inContent:
  365.             if ( whichWindow != FrontWindow() ) {
  366.                 SelectWindow(whichWindow);    gActiveWindow = whichWindow;
  367.             } else {
  368.                 if (whichWindow && whichWindow == (WindowPtr) gDstWindow )  DoPreview(false);
  369.                 }
  370.             break;
  371.         }    /* End Switch() */
  372.         break;
  373.  
  374.     case keyDown:
  375.         if ( ((myEvent->modifiers & cmdKey) != 0) ) {
  376.             res = DoCommand(MenuKey(myEvent->message & charCodeMask));
  377.         } else {
  378.             
  379.             }
  380.         break;
  381.     
  382.     case updateEvt:    DoUpdate((CWindowPtr)myEvent->message);
  383.         break;
  384.     
  385.     case activateEvt:
  386.         whichWindow=(WindowPtr)myEvent->message;
  387.         if ( (myEvent->modifiers & activeFlag) ) {
  388.             gActiveWindow = whichWindow;
  389.             }
  390.         break;
  391.     }        /* End Switch() */
  392.     
  393.     SetGWorld(saveWorld,saveGD);
  394.     return(res);
  395. }    /* End of () */
  396.  
  397.  
  398. /*********************************************
  399.  
  400.     Initialize menu bar.
  401.  
  402. *********************************************/
  403.  
  404. SetupMenus()
  405. {
  406.     gMenus[APPLE_MENU] = GetMenu(APPLE_MENU_ID);
  407.     AddResMenu(gMenus[APPLE_MENU], (ResType) 'DRVR');
  408.     InsertMenu(gMenus[APPLE_MENU], 0);
  409.     gMenus[FILE_MENU] = GetMenu(FILE_MENU_ID);
  410.     InsertMenu(gMenus[FILE_MENU], 0);
  411.     gMenus[EDIT_MENU] = GetMenu(EDIT_MENU_ID);
  412.     InsertMenu(gMenus[EDIT_MENU], 0);
  413.     gMenus[MOVIE_MENU] = GetMenu(MOVIE_MENU_ID);
  414.     InsertMenu(gMenus[MOVIE_MENU], 0);
  415.     gMenus[EFFECT_MENU] = GetMenu(EFFECT_MENU_ID);
  416.     InsertMenu(gMenus[EFFECT_MENU], 0);
  417.     gMenus[Direction_Menu] = GetMenu(Direction_MenuID);
  418.     InsertMenu(gMenus[Direction_Menu], -1);
  419.     DrawMenuBar();
  420.     
  421. }    /* End of () */
  422.  
  423. /*********************************************
  424.  
  425.     Prepare for work.
  426.     
  427. *********************************************/
  428.  
  429. int Initialize()
  430. {    OSErr err;
  431.     Ptr    size;
  432.     long    resp;
  433.     
  434.     size = GetApplLimit();
  435.     SetApplLimit(size - 32*1024);        /* make room on stack so Quickdraw can do big pictures */
  436.     MaxApplZone();
  437.  
  438.     /*    initialize managers */
  439.  
  440.     InitGraf(&qd.thePort);
  441.     InitFonts();
  442.     InitWindows();
  443.     InitMenus();
  444.     InitDialogs(nil);
  445.     InitCursor();
  446.     FlushEvents(everyEvent, 0);
  447.     
  448.     SetupMenus();                        /* Check off correct memus and hilight them */
  449.     
  450.     CouldAlert(140);                    /* Load Alert into Memory ... incase of out of memory errors */
  451.     
  452.  
  453.     if ( NGetTrapAddress(0xab1d,ToolTrap) == NGetTrapAddress(0x9f,ToolTrap) ) {
  454.         return -1;
  455.         }
  456.     if ( Gestalt(gestaltQuickTime, &resp) != 0 ) {
  457.         return -1;
  458.         }
  459.     
  460.     gHasNewStdFile = false;                /* Check for preview and new standard file stuff */
  461.     err = Gestalt(gestaltStandardFileAttr,&resp);
  462.     if ((err == 0) && ((resp & (0x00000001 << gestaltStandardFile58)) != 0))
  463.             gHasNewStdFile = true;
  464.             
  465.     InitSetCompression();                /* Setup default QT compression settings */
  466.           
  467.     return 0;
  468. }    /* End of () */
  469.  
  470.  
  471. /*********************************************
  472.  
  473.     If some kind of fatal error happened come here.
  474.  
  475. *********************************************/
  476.  
  477. void Error(char *msg, OSErr code)
  478. {    Str15 buf;
  479.     GWorldPtr    saveWorld;
  480.     GDHandle    saveGD;
  481.     CGrafPtr    wmgrPort;
  482.     extern char *moviesErrorNames[];
  483.     
  484.     GetGWorld(&saveWorld,&saveGD);    GetCWMgrPort(&wmgrPort);
  485.     SetGWorld(wmgrPort,nil);
  486.     if (code ) {
  487.         NumToString(code, buf);
  488.     } else buf[0] = 0;
  489.     
  490.     c2pstr(msg);
  491.     if (code >= -2051 && code<= -2000) {
  492.         c2pstr(moviesErrorNames[-code - 2000]);
  493.         ParamText((StringPtr)msg,buf,(StringPtr) moviesErrorNames[-code - 2000],0);
  494.         p2cstr((StringPtr)moviesErrorNames[-code - 2000]);
  495.     } else
  496.         ParamText((StringPtr)msg,buf,0,0);
  497.     if ( code == 0 )
  498.         Alert(140,nil);
  499.     else
  500.         StopAlert(140,nil);
  501.     p2cstr((StringPtr) msg);
  502.     
  503.     SetGWorld(saveWorld,saveGD);
  504. }    /* End of () */
  505.  
  506.  
  507. ShowAboutBox()
  508. {
  509.  
  510.     ParamText(    "\pCreated from:\r\rPictMovier & ImportExportMovie QuickTime Sample Code",
  511.                 "\p",
  512.                 "\p\r\rChange By R.Mark Fleming <MarkF@post.QueensU.CA",0);
  513.     Alert(140,nil);
  514. }    /* End of () */
  515.  
  516.  
  517. /********************************************
  518.  
  519.     The main program
  520.     
  521.     - Setup QuickTime movie enviroment
  522.     - Call Initialision routines
  523.     - Call Main Event Handling routines
  524.     - Cleanup QuickTime Movie Enviroment
  525.     
  526. ********************************************/
  527.  
  528. main()
  529. {    EventRecord myEvent;
  530.     
  531.     if (Initialize()) {
  532.         SysBeep(0);    ExitToShell();
  533.         }    
  534.     
  535.     EnterMovies();    
  536.         
  537.     gStandardP.theCodecType = 'smc ';        // graphics compressor is good choice
  538.     if ( gEffectType == EFFECT_StereoGram) {        
  539.         gStandardP.depth = 40;                    // randomdotstereogram needs grayscale buffers
  540.     } else {
  541.         gStandardP.depth = 32;                    // full resolution for best effect
  542.         }
  543.     gRequiresAlternate = 
  544.         (EFFECT_CrossFad == gEffectType) ? true : false ;    // needs two picts to do processing
  545.     
  546.     while ( !gExitFlag ) {
  547.         if ( WaitNextEvent( everyEvent, &myEvent, 1, nil) != 0 ) {
  548.             HandleEvents(&myEvent);
  549.         }
  550.     }
  551.  
  552. done:
  553.     ExitMovies();    
  554.     ExitToShell();
  555. }    /* End of () */
  556.